using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml;
using System.Security.Cryptography;

namespace blogapi
{
    public partial class pages : Form
    {
        public pages(nastaveni nas)
        {
            InitializeComponent();
            nastav = nas;

            mozn = new ArrayList();
            if (nastav.typzapisu == 0)
            {
                if (File.Exists(nastav.nachazitese + "/ucty.db"))
                {
                    bool pokrac = true;
                    FileStream ct = null;
                    BinaryReader rd = null;
                    try
                    {
                        ct = new FileStream(nastav.nachazitese + "/ucty.db", FileMode.Open);
                        rd = new BinaryReader(ct, Encoding.UTF8);
                    }
                    catch
                    {
                        MessageBox.Show(nas.getlang("chyba_nacitani"), nas.getlang("chyba_nacitani_titulek"));
                        pokrac = false;
                    }
                    if (pokrac)
                    {
                        try
                        {
                            while (true)
                            {
                                blogs b = new blogs();
                                b.user = rd.ReadString();
                                b.pass = rd.ReadString();
                                b.url = rd.ReadString();
                                b.type = rd.ReadInt32();
                                b.vybrat = rd.ReadBoolean();
                                b.kategoriezestringu(rd.ReadString());
                                mozn.Add(b);
                            }
                        }
                        catch
                        {
                        }
                        rd.Close();
                        ct.Close();
                    }
                }
            }
            else
            {
                if (File.Exists(nastav.nachazitese + "/data.xml"))
                {
                    XmlTextReader cti = null;
                    try
                    {
                        cti = new XmlTextReader(nastav.nachazitese + "/data.xml");
                        while (!cti.EOF)
                        {
                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "servers")
                            {
                                while (!cti.EOF)
                                {
                                    if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "server")
                                    {
                                        blogs blg = new blogs();
                                        while (!cti.EOF)
                                        {
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "user")
                                            {
                                                blg.user = cti.ReadElementContentAsString();
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "url")
                                            {
                                                blg.url = cti.ReadElementContentAsString();
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "pass")
                                            {
                                                string pom = cti.ReadElementContentAsString();
                                                if (pom != "" || pom!=null)
                                                {
                                                    blg.pass = pom;
                                                }
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "passenc")
                                            {
                                                string pom = cti.ReadElementContentAsString();
                                                if (pom != "" || pom != null)
                                                {
                                                    blg.desifruj(pom);
                                                }
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "cats")
                                            {
                                                while (!cti.EOF)
                                                {
                                                    if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "cat")
                                                    {
                                                        blg.kategorie.Add(cti.ReadElementContentAsString());
                                                        continue;
                                                    }
                                                    if (cti.MoveToContent() == XmlNodeType.EndElement && cti.Name == "cats")
                                                    {
                                                        break;
                                                    }
                                                    cti.Read();
                                                }
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "type")
                                            {
                                                blg.type = cti.ReadElementContentAsInt();
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "chose")
                                            {
                                                blg.vybrat = Convert.ToBoolean(cti.ReadElementContentAsString());
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.EndElement && cti.Name == "server")
                                            {
                                                break;
                                            }
                                            cti.Read();
                                        }
                                        mozn.Add(blg);
                                        continue;
                                    }
                                    if (cti.MoveToContent() == XmlNodeType.EndElement && cti.Name == "servers")
                                    {
                                        break;
                                    }
                                    cti.Read();
                                }
                                break;
                            }
                            else
                                cti.Read();
                        }
                        cti.Close();
                    }
                    catch
                    {
                        MessageBox.Show(nas.getlang("chyba_nacitani"), nas.getlang("chyba_nacitani_titulek"));
                    }
                    if (cti != null)
                    {
                        cti.Close();
                    }
                }
            }

            pr = new pridej(this, nastav);
            setlang();
        }
        nastaveni nastav;
        public ArrayList mozn;

        public void nacti()
        {
            listView1.Items.Clear();
            for (int i = 0; i < mozn.Count; i++)
            {
                blogs bl = (blogs)mozn[i];
                ListViewItem pred = new ListViewItem(bl.tabulka());
                pred.Tag = i;
                listView1.Items.Add(pred);
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        pridej pr;
        private void menuItem3_Click(object sender, EventArgs e)
        {
            pr.Clear();
            pr.Show();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            if (DialogResult.No == MessageBox.Show(nastav.getlang("smazatserv"), nastav.getlang("delpolozka"), MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1))
            {
                return;
            }
            for (int i = listView1.Items.Count - 1; i >= 0; i--)
            {
                if (!listView1.Items[i].Selected)
                {
                    continue;
                }
                int id = (int)listView1.Items[i].Tag;
                mozn.RemoveAt(id);
            }
            nacti();
            MessageBox.Show(nastav.getlang("odstraneniuspesne"), nastav.getlang("delpolozka"));
        }

        public void setlang()
        {
            menuItem1.Text = nastav.getlang("zpet");
            menuItem2.Text = nastav.getlang("moznosti");
            menuItem3.Text = nastav.getlang("newac");
            columnHeader1.Text = nastav.getlang("blog");
            columnHeader2.Text = nastav.getlang("user");
            menuItem4.Text = nastav.getlang("smazat");
            Text = "W - "+nastav.getlang("spravauctu");
            pr.setlang();
        }
    }

    class blogs : ICloneable
    {
        public blogs()
        {
            kategorie = new ArrayList();
            user = "";
            pass = "";
            type = 0;
            vybrat = true;
        }
        public string user;
        public string pass;
        public string url;
        public int type;
        public bool vybrat;
        public ArrayList kategorie;
        private byte[] getklic(int poc)
        {
            string pom = url;
            if (pom == "")
            {
                pom="abdkfqjwehbfdjsbflkaefrlkwgelqkfbdahfb";
            }
            byte[] input = Encoding.UTF8.GetBytes(pom);
            byte[] vys = new byte[poc];
            int p = poc;
            for (int i = 0; i < vys.Length; i++)
            {
                if (p > input.Length-1)
                {
                    p = 0;
                }
                vys[i] = input[p];
                p++;
            }
            return vys;
        }
        public string sifruj()
        {
            RijndaelManaged klic = new RijndaelManaged();
            ICryptoTransform encryptor = klic.CreateEncryptor(getklic(32), getklic(16));
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
            byte[] input = Encoding.UTF8.GetBytes(pass);
            csEncrypt.Write(input, 0, input.Length);
            csEncrypt.FlushFinalBlock();
            return (Convert.ToBase64String(msEncrypt.ToArray()));
        }
        public void desifruj(string passs)
        {
            byte[] clearData = Convert.FromBase64String(passs);
            MemoryStream ms = new MemoryStream();
            Rijndael alg = Rijndael.Create();
            alg.Key = getklic(32);
            alg.IV = getklic(16);
            CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(clearData, 0, clearData.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();
            pass= (Encoding.UTF8.GetString(encryptedData,0,encryptedData.Length));
        }
        public string[] tabulka()
        {
            string[] str = new string[2];
            str[0] = url;
            str[1] = user;
            return str;
        }
        public string kategorietostring()
        {
            string vys = "";
            for (int i = 0; i < kategorie.Count; i++)
            {
                if (vys != "")
                {
                    vys += "|";
                }
                vys += kategorie[i].ToString();
            }
            return vys;
        }
        public void kategoriezestringu(string z)
        {
            kategorie = new ArrayList();
            string[] pol = z.Split('|');
            for (int i = 0; i < pol.Length; i++)
            {
                kategorie.Add(pol[i]);
            }
        }

        public object Clone()
        {
            blogs x = new blogs();
            x.user = (string)this.user.Clone();
            x.pass = (string)this.pass.Clone();
            x.url = (string)this.url.Clone();
            x.type = this.type;
            x.vybrat = this.vybrat;
            x.kategorie = (ArrayList)this.kategorie.Clone();
            return x;
        }
    }
}